package com.wcs.exam.controller.admin.biz;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.wcs.exam.common.base.BaseBiz;
import com.wcs.exam.common.base.Result;
import com.wcs.exam.common.base.page.Page;
import com.wcs.exam.common.base.page.PageUtil;
import com.wcs.exam.common.util.BeanUtil;
import com.wcs.exam.controller.admin.req.AdminUsersLogPageReq;
import com.wcs.exam.controller.admin.resp.AdminUsersLogPageResp;
import com.wcs.exam.controller.admin.resp.AdminUsersLogViewResp;
import com.wcs.exam.dao.UsersDao;
import com.wcs.exam.dao.UsersLogDao;
import com.wcs.exam.dao.impl.mapper.entity.Users;
import com.wcs.exam.dao.impl.mapper.entity.UsersLog;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Admin-用户注册登录日志
 *
 * @author wcs
 * @since 2024-07-06
 */
@Component
@RequiredArgsConstructor
public class AdminUsersLogBiz extends BaseBiz {

    @NotNull
    private final UsersLogDao dao;
    @NotNull
    private final UsersDao usersDao;

    /**
     * 用户注册登录日志-分页
     *
     * @param req 用户注册登录日志-分页查询参数
     * @return 用户注册登录日志
     */
    public Result<Page<AdminUsersLogPageResp>> page(AdminUsersLogPageReq req) {
        // 处理查询条件
        LambdaQueryWrapper<UsersLog> wrapper = new LambdaQueryWrapper<UsersLog>()
                .eq(ObjectUtil.isNotEmpty(req.getLoginStatus()), UsersLog::getLoginStatus, req.getLoginStatus())
                .eq(ObjectUtil.isNotEmpty(req.getLoginClient()), UsersLog::getLoginClient, req.getLoginClient())
                .eq(ObjectUtil.isNotEmpty(req.getLoginType()), UsersLog::getLoginType, req.getLoginType())
                // 排序
                .orderByDesc(UsersLog::getId);

        // 分页查询
        IPage<UsersLog> page = dao.page(PageDTO.of(req.getPageCurrent(), req.getPageSize()), wrapper);
        Page<AdminUsersLogPageResp> respPage = PageUtil.transform(page, AdminUsersLogPageResp.class);
        if (CollectionUtil.isEmpty(respPage.getList())) {
            return Result.success(respPage);
        }


        List<Long> userIdList = respPage.getList().stream().map(AdminUsersLogPageResp::getUserId).collect(Collectors.toList());
        List<Users> usersList = usersDao.listByIds(userIdList);
        Map<Long, Users> usersMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(usersList)) {
            usersList.forEach(item -> usersMap.put(item.getId(), item));
        }

        for (AdminUsersLogPageResp res : respPage.getList()) {
            Users user = usersMap.get(res.getUserId());
            if (ObjectUtil.isNotNull(user)) {
                res.setUserHead(user.getUserHead());
                res.setNickname(user.getNickname());
                res.setMobile(user.getMobile());
                res.setMobile(DesensitizedUtil.mobilePhone(user.getMobile()));
            }
        }
        return Result.success(respPage);
    }


    /**
     * 用户注册登录日志-查看
     *
     * @param id 主键ID
     * @return 用户注册登录日志
     */
    public Result<AdminUsersLogViewResp> view(Long id) {
        UsersLog usersLog = dao.getById(id);
        if (ObjectUtil.isNull(usersLog)) {
            return Result.error("用户注册登录日志不存在");
        }
        AdminUsersLogViewResp resp = BeanUtil.copyProperties(usersLog, AdminUsersLogViewResp.class);

        Users user = usersDao.getById(usersLog.getUserId());
        if (ObjectUtil.isNotNull(user)) {
            resp.setUserHead(user.getUserHead());
            resp.setNickname(user.getNickname());
            resp.setMobile(user.getMobile());
            resp.setMobile(DesensitizedUtil.mobilePhone(user.getMobile()));
        }
        return Result.success(resp);
    }

    /**
     * 用户注册登录日志-删除
     *
     * @param id 主键ID
     * @return 删除结果
     */
    public Result<String> delete(Long id) {
        // 获取校验用户注册登录日志
        UsersLog usersLog = dao.getById(id);
        if (ObjectUtil.isNull(usersLog)) {
            return Result.error("用户注册登录日志不存在");
        }

        // 删除用户注册登录日志
        boolean deleteRes = dao.removeById(id);
        return deleteRes ? Result.success(toI18nSuccess()) : Result.error(toI18nFail());
    }
}
